﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Service.Interface;
using Domain;
using Clannan.ActionFilter;
using Clannan.Models.JsonModel;

namespace Clannan.Controllers
{
    public class NoteBookController : Controller
    {
        public IUserInfoService UserInfoService;
        public INoteBookService NotebookService;
        public IAreaService AreaService;
        public IPageService PageService;
        public IMemoService MemoService;
        public IMobilePageService MobilePageService;
        public IMobileImageService MobileImageService;

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Test()
        {

            return View("Index_01");
        }

        /// <summary>
        /// 异步获取用户笔记本、分区数据
        /// </summary>
        /// <returns></returns>
        public JsonResult GetNoteBookListAndAreaList()
        {
            List<NoteBookJsonModel> notebookList = new List<NoteBookJsonModel>();//Json返回笔记本集合
            List<AreaJsonModel> areaList = new List<AreaJsonModel>();            //Json返回分区集合
            List<PageJsonModel> pageList = new List<PageJsonModel>();            //Json返回分区集合
            int pageCount; //返回页的总页数
            NoteBookJsonModel notebookJsonModel;//笔记本Json模型
            AreaJsonModel areaJsonModel;        //分区Json模型
            PageJsonModel pageJsonModel;        //页Json模型

            //获取用户笔记本数据
            IList<NoteBook> userNoteBooklist = this.NotebookService.LoadAllNoteBook(5, false);
            //数据处理
            foreach (NoteBook notebookItem in userNoteBooklist)
            {
                ///获取笔记本集合
                notebookJsonModel = new NoteBookJsonModel();
                notebookJsonModel = notebookJsonModel.Copy(notebookJsonModel, notebookItem);
                notebookList.Add(notebookJsonModel);

                ///获取分区集合
                foreach (Area areaItem in notebookItem.Areas)
                {
                    if (areaItem.MarkDelete == false)
                    {
                        areaJsonModel = new AreaJsonModel();
                        areaJsonModel = areaJsonModel.Copy(areaJsonModel, areaItem);
                        areaList.Add(areaJsonModel);
                    }
                }
            }
            //获取笔记本ID排序字符串
            string notebookSort = this.UserInfoService.Get(5).NoteBookSort;

            //获取第一个笔记本的第一个分区的页集合
            string[] notebookSortArray = notebookSort.Split('-');
            NoteBook firstNoteBook = this.NotebookService.Get(notebookSortArray[0]);//获取第一个笔记本
            string[] areaSortArray = firstNoteBook.AreaSort.Split('-');
            Area firstArea = this.AreaService.Get(areaSortArray[0]);////获取第一个笔记本的第一个分区
            IList<Page> pageListData = this.PageService.LoadAllByPage(firstArea.ID, 1, 50, out pageCount, firstArea.PageSortType);
            //数据处理
            foreach (Page pageItem in pageListData)
            {
                pageJsonModel = new PageJsonModel();
                pageJsonModel = pageJsonModel.Copy(pageJsonModel, pageItem);
                pageList.Add(pageJsonModel);
            }

            var result = new
            {
                NoteBookList = notebookList,
                AreaList = areaList,
                NoteBookSort = notebookSort,
                PageList = pageList,
                PageCount = pageCount,
                Message = 1
            };

            return Json(result);
        }

        /// <summary>
        /// 更新用户笔记本排列顺序
        /// </summary>
        /// <param name="notebookSort">笔记本ID排列顺序字符串</param>
        /// <returns></returns>
        public JsonResult UpdateNoteBookSort(string notebookSort)
        {
            UserInfo userInfo = this.UserInfoService.Get(5);//User.Identity.Name
            userInfo.NoteBookSort = notebookSort;
            this.UserInfoService.Update(userInfo);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 更新笔记本分区顺序
        /// </summary>
        /// <param name="notebookID">笔记本ID</param>
        /// <param name="areaSort">分区ID排序顺序字符串</param>
        /// <returns></returns>
        public JsonResult UpdateAreaSort(string notebookID, string areaSort)
        {
            NoteBook notebook = this.NotebookService.Get(notebookID);
            notebook.AreaSort = areaSort;
            this.NotebookService.Save(notebook);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 为当前用户添加新笔记本
        /// </summary>
        /// <param name="notebookName">笔记本名称</param>
        /// <returns></returns>
        public JsonResult Create(string notebookName)
        {
            NoteBook notebook = this.NotebookService.SaveNoteBookAndArea(notebookName, Convert.ToInt32(5));//User.Identity.Name
            int areaID = Convert.ToInt32((notebook.AreaSort.Split('-'))[0]);

            return Json(new { Message = 1, NoteBookID = notebook.ID, AreaID = areaID });
        }

        /// <summary>
        /// 将笔记本扔进垃圾箱
        /// </summary>
        /// <param name="notebookID">笔记本ID</param>
        /// <returns></returns>
        public JsonResult Throw(string notebookID)
        {
            this.NotebookService.UpdateNoteBookMarkDelete(notebookID, true);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 从垃圾箱中还原笔记本
        /// </summary>
        /// <param name="notebookID">笔记本ID</param>
        /// <returns></returns>
        public JsonResult Restore(string notebookID)
        {
            this.NotebookService.UpdateNoteBookMarkDelete(notebookID, false);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 更新笔记本名称
        /// </summary>
        /// <param name="notebookID">笔记本ID</param>
        /// <param name="notebookName">笔记本名称</param>
        /// <returns></returns>
        public JsonResult Update(string notebookID, string notebookName)
        {
            NoteBook notebook = this.NotebookService.Get(notebookID);
            notebook.Name = notebookName;
            this.NotebookService.Update(notebook);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 右键直删除笔记本
        /// </summary>
        /// <param name="notebookID">笔记本ID</param>
        /// <returns></returns>
        public JsonResult Delete(string notebookID)
        {
            this.NotebookService.Delete(notebookID);

            return Json(new { Message = 1 });
        }
        
        /// <summary>
        /// 共享笔记本,包括共享笔记本的分区，分区中的页
        /// </summary>
        /// <param name="notebookID">笔记本ID</param>
        /// <returns></returns>
        public JsonResult Share(string notebookID)
        {
            this.NotebookService.UpdateNoteBookShare(notebookID, true);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 取消共享笔记本
        /// </summary>
        /// <param name="notebookID">笔记本ID</param>
        /// <returns></returns>
        public JsonResult CancelShare(string notebookID)
        {
            this.NotebookService.UpdateNoteBookShare(notebookID, false);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 清空垃圾箱
        /// </summary>
        /// <returns></returns>
        public JsonResult ClearDustbin()
        {
            this.NotebookService.DeleteAllMarkDelete(5);//User.Identity.Name

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 还原垃圾箱
        /// </summary>
        /// <returns></returns>
        public JsonResult RestoreDustbin()
        {
            this.NotebookService.UpdateAllMarkDeleteEqualsTrue(5);//User.Identity.Name

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 获取用户垃圾箱中的数据
        /// </summary>
        /// <returns></returns>
        public JsonResult GetAllDustbin()
        {
            List<NoteBookJsonModel> notebookList = new List<NoteBookJsonModel>();         //Json返回笔记本集合
            List<AreaJsonModel> areaList = new List<AreaJsonModel>();                     //Json返回分区集合
            List<PageJsonModel> pageList = new List<PageJsonModel>();                     //Json返回页集合
            List<MemoJsonModel> memoList = new List<MemoJsonModel>();                     //Json返回备忘录集合
            List<MobilePageJsonModel> mobilepageList = new List<MobilePageJsonModel>();   //Json返回手机页集合
            List<MobileImageJsonModel> mobileimageList = new List<MobileImageJsonModel>();//Json返回手机页集合
            NoteBookJsonModel notebookJsonModel;      //笔记本Json模型
            AreaJsonModel areaJsonModel;              //分区Json模型
            PageJsonModel pageJsonModel;              //页Json模型
            MemoJsonModel memoJsonModel;              //备忘录Json模型
            MobilePageJsonModel mobilepageJsonModel;  //手机页Json模型
            MobileImageJsonModel mobileImageJsonModel;//手机页Json模型

            IList<NoteBook> userNoteBooklist = this.NotebookService.LoadAllNoteBook(User.Identity.Name, true);
            foreach (NoteBook notebook in userNoteBooklist)
            {
                notebookJsonModel = new NoteBookJsonModel();
                notebookJsonModel = notebookJsonModel.Copy(notebookJsonModel, notebook);
                notebookList.Add(notebookJsonModel);
            }
            IList<Area> userAreaList = this.AreaService.LoadAllArea(User.Identity.Name, true);
            foreach (Area area in userAreaList)
            {
                areaJsonModel = new AreaJsonModel();
                areaJsonModel = areaJsonModel.Copy(areaJsonModel, area);
                areaList.Add(areaJsonModel);
            }
            IList<Page> userPageList = this.PageService.LoadAllPage(User.Identity.Name, true);
            foreach (Page page in userPageList)
            {
                pageJsonModel = new PageJsonModel();
                pageJsonModel = pageJsonModel.Copy(pageJsonModel, page);
                pageList.Add(pageJsonModel);
            }
            IList<Memo> userMemoList = this.MemoService.LoadAllMemo(User.Identity.Name, true);
            foreach (Memo memo in userMemoList)
            {
                memoJsonModel = new MemoJsonModel();
                memoJsonModel = memoJsonModel.Copy(memoJsonModel, memo);
                memoList.Add(memoJsonModel);
            }
            IList<MobilePage> userMobilePageList = this.MobilePageService.LoadAllMobilePage(User.Identity.Name, true);
            foreach (MobilePage mobilepage in userMobilePageList)
            {
                mobilepageJsonModel = new MobilePageJsonModel();
                mobilepageJsonModel = mobilepageJsonModel.Copy(mobilepageJsonModel, mobilepage);
                mobilepageList.Add(mobilepageJsonModel);
            }
            IList<MobileImage> userMobileImageList = this.MobileImageService.LoadAllMobilePage(User.Identity.Name, true);
            foreach (MobileImage mobileimage in userMobileImageList)
            {
                mobileImageJsonModel = new MobileImageJsonModel();
                mobileImageJsonModel = mobileImageJsonModel.Copy(mobileImageJsonModel, mobileimage);
                mobileimageList.Add(mobileImageJsonModel);
            }

            var result = new 
            {
                NoteBookList = notebookList,
                AreaList = areaList,
                PageList = pageList,
                MemoList = memoList,
                MobilePageList = mobilepageList,
                MobileImageList = mobileimageList
            };

            return Json(result);
        }

    }

}
